<?php

namespace App\Admin\Controllers;

use App\Models\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Show;
use DB;

class UsersController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户列表');
            //$content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            //->description('description')
            ->body($this->detail($id));
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
                //$tools->disableList();
                $tools->disableDelete();
            });

        $show->wechat_nickname('微信昵称');
        $show->wechat_avatar('微信头像')->image();
        $show->telephone('绑定手机号');
        $show->is_vip('是否会员')->as(function($value) {
            if($value == 0)
            {
                return '非会员';
            }
            elseif($value == 1)
            {
                return '是会员';
            }
            else
            {
                return '已过期会员';
            }
        });
        $show->field('is_open_vip','是否已开卡计时')->as(function () use ($id){
            if(!empty($this->vip_start_time) && !empty($this->vip_end_time))
            {
                return '已开卡';
            }
            else
            {
                return '未开卡';
            }
        });

        $show->field('vip_days_total','会员总时长')->as(function () {
            if($this->is_vip == 1)
            {
                if(!empty($this->vip_end_time) && !empty($this->vip_start_time))
                {
                    if($this->vip_end_time >= $this->vip_start_time)
                    {
                        $timediff = $this->vip_end_time - $this->vip_start_time;
                        $day = ceil($timediff/86400);
                    }
                }
                else
                {
                    $day = DB::table('user_vip_orders')->where('user_id',$this->id)->where('is_used',0)->where('status',1)->sum('vip_days');
                }
                return $day;
            }
            return 0;
        });
        $show->field('vip_days','剩余会员时长')->as(function () {
            if($this->is_vip == 1)
            {
                if(!empty($this->vip_end_time) && !empty($this->vip_start_time))
                {
                    $now = time();
                    if($this->vip_end_time >= $now)
                    {
                        $timediff = $this->vip_end_time - $now;
                        $day = ceil($timediff/86400);
                    }
                }
                else
                {
                    $day = DB::table('user_vip_orders')->where('user_id',$this->id)->where('is_used',0)->where('status',1)->sum('vip_days');
                }
                return $day;
            }
            return 0;
        });
        $show->created_at('账户创建时间');
        $show->field('share_users_total','共邀请好友')->as(function () {
            return DB::table('users')->where('share_user_id',$this->id)->count();
        });
        $show->field('share_users_vip','邀请的好友办会员人数')->as(function () {
            return DB::table('users')->where('share_user_id',$this->id)->where('is_vip',1)->count();
        });
        $show->field('share_users_vip_days','通过邀请获得会员奖励')->as(function () {
            return DB::table('user_vip_time_records')->where('user_id',$this->id)->where('type',4)->sum('days');
        });

        $show->divider();

        /*$show->share_users('推荐的用户', function ($users) {
            $users->resource('/admin/users');

            $users->wechat_nickname('微信昵称');
            $users->wechat_avatar('微信头像')->image('',50,50);
            $users->telephone('绑定手机号');
            $users->is_vip('是否会员')->display(function($value) {
                if($value == 0)
                {
                    return '非会员';
                }
                elseif($value == 1)
                {
                    return '是会员';
                }
                else
                {
                    return '已过期会员';
                }
            });

            $users->disableCreateButton();
            $users->disableExport();
            $users->disableRowSelector();
            $users->disableActions();
            $users->disableFilter();
        });*/

        $show->user_vip_time_records('会员时长明细', function ($users) {

            $users->days('天数');
            $users->intro('说明');

            $users->disableCreateButton();
            $users->disableExport();
            $users->disableRowSelector();
            $users->disableActions();
            $users->disableFilter();
        });

        return $show;
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->model()->where('user_type', 1);
            //$grid->id('ID')->sortable();
            $grid->wechat_nickname('微信昵称');
            $grid->wechat_avatar('微信头像')->image('',50,50);
            $grid->telephone('绑定手机号');
            $grid->is_vip('是否会员')->display(function($value) {
                if($value == 0)
                {
                    return '非会员';
                }
                elseif($value == 1)
                {
                    return '是会员';
                }
                else
                {
                    return '已过期会员';
                }
            });
            // 不存的字段列
            $grid->column('vip_days','剩余天数')->display(function () {
                $open = '(未开卡)';
                if($this->is_vip == 1)
                {
                    if(!empty($this->vip_end_time) && !empty($this->vip_start_time))
                    {
                        $now = time();
                        if($this->vip_end_time >= $now)
                        {
                            $timediff = $this->vip_end_time - $now;
                            $day = ceil($timediff/86400);
                        }
                        $open = '(已开卡)';
                    }
                    else
                    {
                        $day = DB::table('user_vip_orders')->where('user_id',$this->id)->where('is_used',0)->where('status',1)->sum('vip_days');
                    }
                    return $day.$open;
                }
                return "0{$open}";
            });


            $grid->created_at('创建时间');

            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('is_vip','是否会员')->select(['1'=>'是会员','0'=>'非会员','-1'=>'已过期会员']);
            });

            /*$grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('is_vip','是否会员')->select(['1'=>'是会员','0'=>'非会员','-1'=>'已过期会员']);

            });*/

            $grid->actions(function ($actions) {
                //$actions->disableDelete();
                $actions->disableEdit();
                //$actions->disableView();
            });
            //$grid->updated_at();
        });
    }

    public function destroy($id)
    {
        $arr = explode(',',$id);
        User::query()->whereIn('id',$arr)->update(['is_vip'=>0,'vip_end_time'=>0,'vip_start_time'=>0,'telephone'=>'']);
        return ['status'=>true,'message'=>'删除成功'];
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
